#include "cell_bookmgr.h"
#include "ui_cell_bookmgr.h"
#include "lib/sqlmgr.h"
#include "dlg_bookam.h"
#include "dlg_bookgs.h"
#include "dlg_bookret.h"

#include <QMessageBox>

Cell_BookMgr::Cell_BookMgr(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Cell_BookMgr)
{
    ui->setupUi(this);
    ui->tableView->setModel(&m_model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置图表不可编辑
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //只允许选中行
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); //最多选中一行
}

void Cell_BookMgr::initPage(QString strCondition)
{
    //查询数据库并显示
    auto l = SqlMgr::getInstance()->getBooks(strCondition);
    //将数据放入model以在界面中显示
    m_model.clear();
    m_model.setHorizontalHeaderLabels(QStringList{"书籍id", "书籍名称", "价格", "类型1", "类型2", "类型3", "数量", "图片"});
    for(int i = 0; i < l.size(); ++i)
    {
        QList<QStandardItem*> items;
        for(int j = 0; j < l[i].size(); ++j) {
            items.append(new QStandardItem(l[i][j]));
        }
        m_model.appendRow((items));
    }
}

Cell_BookMgr::~Cell_BookMgr()
{
    delete ui;
}

void Cell_BookMgr::on_le_search_textChanged(const QString &arg1)
{
    initPage(arg1);
}

//图书添加
void Cell_BookMgr::on_btn_add_clicked()
{
    Dlg_AM dlg;
    dlg.exec();
    initPage();
}

//图书修改
void Cell_BookMgr::on_btn_modify_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r < 0) {
        QMessageBox::information(nullptr, "信息", "无选中图书!   ");
    }
    else
    {
        auto id = m_model.item(r, 0)->text();
        Dlg_AM dlg;
        dlg.setType(id.toInt());
        dlg.exec();
        initPage();
    }
}

//图书删除
void Cell_BookMgr::on_btn_del_clicked()
{
    //删除用户
    int r = ui->tableView->currentIndex().row();
    if(r<0) {
        QMessageBox::information(nullptr, "信息", "无选中图书!   ");
    }
    else
    {
        auto id = m_model.item(r,0)->text();
        auto str = SqlMgr::getInstance()->delBook(id);
        QMessageBox::information(nullptr, "信息", str.isEmpty() ? "删除成功   " : str);
        initPage();
    }
}

//图书归还
void Cell_BookMgr::on_ptn_return_clicked()
{
    int r = ui->tableView->currentIndex().row();
    auto id = m_model.item(r,0)->text();
    dlg_bookgs dlg;
    dlg.setType(true);
    dlg.setBookId(id.toInt());
    int ret = dlg.exec();
    if(ret == 0) {
        QMessageBox::information(nullptr, "信息", "归还失败   ");
    }
    else if(ret == 1) {
        QMessageBox::information(nullptr, "信息", "归还成功   ");
    }
    else if(ret == 2) { dlg.hide(); }
    if(ret) { initPage(); }
}

void Cell_BookMgr::on_btn_borrow_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r<0)
    {
        QMessageBox::information(nullptr, "信息", "未选择图书!  ");
        return;
    }
    auto id = m_model.item(r,0)->text();
    auto cnt = m_model.item(r,6)->text().toInt();
    if(cnt <= 0)
    {
        QMessageBox::information(nullptr, "信息", "借阅失败,没有库存   ");
        return;
    }
    dlg_bookgs dlg;

    dlg.setBookId(id.toInt());
    int ret = dlg.exec();
    if(ret == 0) {
        QMessageBox::information(nullptr, "信息", "借阅失败   ");
    }
    else if(ret == 1) {
        QMessageBox::information(nullptr, "信息", "借阅成功   ");
    }
    else if(ret == 2) { dlg.hide(); }
    if(ret) { initPage(); }
}

